//
//  CZStatusBottomView.swift
//  GZWeibo666
//
//  Created by Apple on 15/11/28.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

// MARK: - 按钮点击事件
/// cell底部视图按钮点击事件
protocol CZStatusBottomViewDelegate: NSObjectProtocol {
    func forwardButtonClick()
    
    func commentButtonClick()
    
    func lickButtonClick()
}

class CZStatusBottomView: UIView {
    
    // MARK: - 属性
    /// 代理
    weak var delegate: CZStatusBottomViewDelegate?
    
    /// 微博模型
    var status: CZStatus? {
        didSet {
            setButton(forwardButton, count: status!.reposts_count)
            setButton(commentButton, count: status!.comments_count)
            setButton(likeButton, count: status!.attitudes_count)
            
//            setButton(forwardButton, count: 8888)
//            setButton(commentButton, count: 10000)
//            setButton(lickButton, count: 12000)
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }

    // MARK: - 准备UI
    private func prepareUI() {
        // 添加点击事件
        forwardButton.addTarget(self, action: "clickForwardButton", forControlEvents: UIControlEvents.TouchUpInside)
        commentButton.addTarget(self, action: "clickCommentButton", forControlEvents: UIControlEvents.TouchUpInside)
        likeButton.addTarget(self, action: "clickLickButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        backgroundColor = UIColor(white: 0.98, alpha: 1)
        
        // 添加子控件
        addSubview(forwardButton)
        addSubview(commentButton)
        addSubview(likeButton)
        addSubview(sImageViewFirst)
        addSubview(sImageViewSecond)
        
        // 添加约束
        /// 平铺子控件
        /// self: 父控件
        /// views: 哪些子控件要平铺
        /// insets边距
        self.ff_HorizontalTile([forwardButton, commentButton, likeButton], insets: UIEdgeInsetsZero)
        
        sImageViewFirst.ff_AlignHorizontal(type: ff_AlignType.CenterRight, referView: forwardButton, size: nil)
        
        sImageViewSecond.ff_AlignHorizontal(type: ff_AlignType.CenterRight, referView: commentButton, size: nil)
    }
    
    // MARK: - 设置按钮文字
    private func setButton(button: UIButton, count: Int) {
        if count >= 10000 {
            let final = CGFloat(count) / 10000.0
            var titleText = String(format: "%.1f万", arguments: [final])
            titleText = titleText.stringByReplacingOccurrencesOfString(".0", withString: "")
            
            button.setTitle(titleText, forState: UIControlState.Normal)
        } else if count > 0 {
            button.setTitle("\(count)", forState: UIControlState.Normal)
        }
    }
    
    // MARK: - 按钮点击事件
    func clickForwardButton() {
        delegate?.forwardButtonClick()
    }
    
    func clickCommentButton() {
        delegate?.commentButtonClick()
    }
    
    func clickLickButton() {
        delegate?.lickButtonClick()
    }
    
    // MARK: - 懒加载
    /// 转发按钮
    private lazy var forwardButton: UIButton = UIButton(imageName: "timeline_icon_retweet", title: "转发")
    
    /// 评论按钮
    private lazy var commentButton: UIButton = UIButton(imageName: "timeline_icon_comment", title: "评论")
    
    /// 赞按钮
    private lazy var likeButton: UIButton = UIButton(imageName: "timeline_icon_unlike", title: "赞")
    
    /// 分割线1
    private lazy var sImageViewFirst: UIImageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line_highlighted"))
    
    /// 分割线2
    private lazy var sImageViewSecond: UIImageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line_highlighted"))
}
